<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.SQLException" %>
<%@ page import="java.sql.Statement" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page language="java" import="java.lang.*" %>
<%@ page import = "java.sql.SQLException" %>
<%@ page import = "com.seniorproject.aims.*" %>
<%@ page import = "java.util.List" %>
<%@ page import ="java.util.ArrayList"%>
<%@ page import = "java.util.Properties" %>
<%@ page import = "javax.servlet.ServletContext" %>
<%@ page import = "java.io.File" %>
<%@ page import = "java.io.InputStream" %>


<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>Department</title>
	
	<link rel="stylesheet" type="text/css" href="css/stylev2.css" /> 
	<link rel="stylesheet" type="text/css" href="css/student-system.css" />
	<link rel="stylesheet" type="text/css" href="css/menubar.css" />
	<link rel="stylesheet" type="text/css" href="css/frame.css" />
	
	<style type="text/css">
		a {
	      text-decoration:none;
	   }
	</style>
	
	<script>
		
		function deleteButton(id) {
			var departmentNameTH = (document.getElementById('departmentNameTH' + id)).innerHTML.trim();
			var departmentId = document.getElementById('departmentId' + id).value;
						
			if (confirm("คุณต้องการจะลบข้อมูลของภาควิชา " + departmentNameTH) == true) {
				/* AJAX */
				var xmlhttp;
				
				if(window.XMLHttpRequest) {
					// code for IE7+, Firefox, Chrome, Opera, Safari
					xmlhttp = new XMLHttpRequest();
				}
				else {
					// code for IE6, IE5
					xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
				}
				
				xmlhttp.onreadystatechange = function() {
					if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
						var result = xmlhttp.responseText;

						if(result == "success") {
							window.location.reload();
							alert("สำเร็จ");
						}
						else if(result == "fail") {
							alert("ไม่สามารถลบภาควิชา "+departmentNameTH+" ได้ เนื่องจากภาควิชานี้มีการใช้งานอยู่");
						}
						else {
							alert("ไม่สามรถลบข้อมูลได้ค่ะ กรุณาลองใหม่อีกครั้ง");
						}
						
					}
				}
				
				xmlhttp.open("POST", "admin_department_delete.jsp?departmentId="+departmentId, true);
				xmlhttp.send();
		    } else { }
		}
		
		function add_department() {
			document.location.href = "admin_department_add.jsp";
		}
	</script>
</head>

<body>

<%
	// get session
	String strUser = String.valueOf(session.getAttribute("sUser"));
	HttpSession htp_session = request.getSession();
	
	// check session
		if (htp_session == null || htp_session.getAttribute("sUser") == null) {
		    // Forward the control to login.jsp if authentication fails or session expires
		    request.getRequestDispatcher("/login.jsp").forward(request,
		        response);
		}
		if(!"admin".equals(htp_session.getAttribute("sUser"))){%>
			<script>alert("This user dont have PERMISSION to access this zone.");</script>
			<meta HTTP-EQUIV="Refresh" CONTENT="0; URL=system_select.jsp"> 	
	<%}
	
	
	//set Database Connection
	String hostProps = "";
	String usernameProps  = "";
	String passwordProps  = "";
	String databaseProps = "";
	
	try {
		//get current path
		ServletContext servletContext = request.getSession().getServletContext();
		
		InputStream input = servletContext.getResourceAsStream("/properties/connectDB.properties");
		Properties props = new Properties();
		
		props.load(input);

		hostProps  = props.getProperty("host");
		usernameProps  = props.getProperty("username");
		passwordProps  = props.getProperty("password");
		databaseProps = props.getProperty("database");
	} catch (Exception e) { 
		out.println(e);  
	}
	
	// connect database
	Connection connect = null;		
	try {
		Class.forName("com.mysql.jdbc.Driver");
	
		connect =  DriverManager.getConnection("jdbc:mysql://" + hostProps  + "/" + databaseProps +
				"?user=" + usernameProps  + "&password=" + passwordProps );
	
		if(connect != null){
			System.out.println("Database Connect Sucesses.");
		} else {
			System.out.println("Database Connect Failed.");	
		}

	} catch (Exception e) {
		out.println(e.getMessage());
		e.printStackTrace();
	}
	
	/* find department in database */
	List<Department> department = new ArrayList<Department>();
	
	try {			   
		ResultSet rs = connect.createStatement().executeQuery("SELECT department_id, department_name_th, department_name_en, department_name_short " 
				+ " FROM department " + " ORDER BY department_name_en");
		
		while(rs.next()) {
			Department depart = new Department();
			
			depart.setDepartmentId(Integer.parseInt(rs.getString("department_id")));
			depart.setDepartmentNameTH(rs.getString("department_name_th"));
			depart.setDepartmentNameEN(rs.getString("department_name_en"));
			depart.setDepartmentNameShort(rs.getString("department_name_short"));
			
			department.add(depart);
		}
		
	} catch (SQLException e) {
		e.printStackTrace();
	}
	
	connect.close();
				
%>

	<div id="page-wrap">
		<div id="inside">
			<table border="0">
				<tr style="font-size:24px">
					<td width="600" height="100" style= "background-color: #273b80;border: 0px ;padding: 0px; 0px;" >
						<div id="header_banner-2"></div>
					</td>
					
					<td width="680" height="100" style= "background-color: #273b80;border: 0px;padding: 0px; 0px;" >
						<p align="right"><label><font size="4" color="#ffffff">username : <%=strUser%>&nbsp;&nbsp;</font></label></p>
						<a href="logout.jsp"><p align="right"><label style="cursor: pointer;"><font size="3" color="#ffffff">log out &nbsp;</font></label></p></a>
					</td>
				</tr>
			</table>
			
			<ul id="drop-nav">
		        <li><a href="admin_home.jsp">หน้าแรก</a> </li>
		        
		        <li>
		            <span><a>องค์กร &nbsp; &#9662;</a></span>
		            <ul>
		                <li><a href="admin_department.jsp">ภาควิชา</a></li>
		                <li><a href="admin_workgroup.jsp">หน่วยงาน</a></li>
		            </ul>
		        </li>
		        <li><a href="admin_role.jsp">หน้าที่</a></li>
		        <li><a href="admin_account.jsp">บัญชีผู้ใช้</a></li>
		        <li>
		        	<a>บุคคล &nbsp; &#9662;</a>
		        	<ul>
		                <li><a href="admin_lecturer.jsp">อาจารย์</a></li>
		                <li><a href="admin_employee.jsp">บุคลากร</a></li>
		            </ul>
		        </li>
		        <li><a href="admin_entrance.jsp">ระบบเข้าศึกษา</a></li>
		        <li><a href="admin_status.jsp">สถานะของนักศึกษา</a></li>
		        <li>
		        	<a href="">ตำแหน่ง &nbsp; &#9662;</a>
		        	<ul>
		                <li><a href="admin_educate_position.jsp">ตำแหน่งทางวิชาการ</a></li>
		                <li><a href="admin_management_position.jsp">ตำแหน่งบริหาร</a></li>
		            </ul>
		        </li>
		    </ul>	
		
				<div id="main-content-ac">
					
						<br/><br/><br/><br/>
						
						<table style="border: 0px">
							<tr style="border: 0px">
								<td style="border: 0px">
									<div class="imageFrame">ภาควิชา</div>
								</td>
								<td style="border: 0px">
									<div style="margin-top: 30px; margin-left: 20px; cursor: pointer;">
										<span title="เพิ่มข้อมูล">
											<img src="image/add_icon.png" width="40px" height="40px" onclick="add_department()">	
										</span>
									</div>
								</td>
							</tr>
						</table>
						
						<br/><br/><br/><br/>
						<center>
							<div class="CSSTableGenerator">							
								<table border="1">
									  <tr style="font-size:24px">
										<td><center><strong>&nbsp;</strong></center></td>
										<td><center><strong>Department</strong></center></td>
										<td><center><strong>ภาควิชา</strong></center></td>
										<td><center><strong>ชื่อย่อ</strong></center></td>
										<td>&nbsp;</td>
										<td>&nbsp;</td>
									  </tr>
								 

<%
				int departmentCount = 1;
				for(Department dep : department) {
					
					if(dep.getDepartmentId() == 0) {
						
					}
					else {
%>
									<tr style="font-size:20px" id="department<%=departmentCount%>">
										<form id="form<%=departmentCount%>" name="form<%=departmentCount%>" method="post" action="admin_department_edit.jsp">
											
											<input type="hidden" id="departmentId" name="departmentId" value="<%=dep.getDepartmentId()%>" />
											<input type="hidden" id="departmentId<%=departmentCount%>" name="departmentId<%=departmentCount%>" value="<%=dep.getDepartmentId()%>" /> 
											<td id="departmentCount" name="departmentCount" align="center">
												<%=departmentCount%>
											</td>
											
											<input type="hidden" id="departmentNameEN" name="departmentNameEN" value="<%=dep.getDepartmentNameEN()%>" /> 
											<td id="departmentNameEN<%=departmentCount%>" name="departmentNameEN<%=departmentCount%>">
												<%=dep.getDepartmentNameEN()%>
											</td>
											
											<input type="hidden" id="departmentNameTH" name="departmentNameTH" value="<%=dep.getDepartmentNameTH()%>" /> 
											<td id="departmentNameTH<%=departmentCount%>" name="departmentNameTH<%=departmentCount%>">
												<%=dep.getDepartmentNameTH()%>
											</td>
											
											<input type="hidden" id="departmentNameShort" name="departmentNameShort" value="<%=dep.getDepartmentNameShort()%>" /> 
											<td id="departmentNameShort<%=departmentCount%>" name="departmentNameShort<%=departmentCount%>">	
												<%=dep.getDepartmentNameShort()%>
											</td>	
											<td>
												<center><span title="แก้ไขข้อมูล">
													<input type="image" src="image/edit_icon.png" height="30px" width="30px" />
												</span></center>
											</td>
										</form>
											<td>
												<center><span title="ลบข้อมูล">
													<input type="image" src="image/delete_icon.png" height="30px" width="30px"  onclick="deleteButton('<%=departmentCount%>')" />
												</span></center>
											</td>
										
									</tr>

<%
					}
					departmentCount++;
				}
				
%>							  
									
								</table>
							</div>
						</center> 
					<br><br><br><br><br><br>
			
				<div style="clear: both;"></div>
					<div id="footer"></div>
		</div>		
				<div style="clear: both;"></div>		
	</div>
	
</body>
</html>